//
//  ContractMenuViewController.m
//  EPBPro
//
//  Created by wucha on 2018/6/25.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "ContractMenuViewController.h"
#import "MyCollectionViewCell.h"
#import "MenuItem.h"
#import "CustomerContractViewController.h"
#import "ShopTypeContractViewController.h"
#import "AddContractViewController.h"
#import "IncomeViewController.h"

@interface ContractMenuViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>{
    UICollectionView *mainCollectionView;
}
@property (nonatomic,strong) NSMutableArray *listArray;

@end

@implementation ContractMenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleName = @"合同菜单";
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    //1.初始化layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置collectionView滚动方向
    //    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    //设置headerView的尺寸大小
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 100);
    //该方法也可以设置itemSize
    layout.itemSize =CGSizeMake(110, 150);
    
    //2.初始化collectionView
    mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, StatusBarHeight+44, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarHeight-44-TabBarHeight) collectionViewLayout:layout];
    [self.view addSubview:mainCollectionView];
    mainCollectionView.backgroundColor = [UIColor clearColor];
    
    //3.注册collectionViewCell
    //注意，此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
    [mainCollectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
    
    //注册headerView  此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致  均为reusableView
    [mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];
    
    //4.设置代理
    mainCollectionView.delegate = self;
    mainCollectionView.dataSource = self;
    NSArray *nameArray = @[@{@"name":@"广告商合同",@"image":@"guanggaoshang_contract"},@{@"name":@"代理商合同",@"image":@"dailishang_contract"},@{@"name":@"渠道商合同",@"image":@"qudaoshang_contract"},@{@"name":@"客户合同",@"image":@"kehu_contract"}];
    NSMutableArray *itemArray = [NSMutableArray array];
    for (NSDictionary *nameDic in nameArray) {
        MenuItem *menuItem = [[MenuItem alloc] init];
        menuItem.name = nameDic[@"name"];
        menuItem.url = nameDic[@"image"];
        [itemArray addObject:menuItem];
    }
    self.listArray = itemArray;
    [mainCollectionView reloadData];
    
    if ([Utils getUserPermission:@"addcontract"]) {
        UIButton *rightbutton  = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 44)];
        [rightbutton setTitle:@"+" forState:UIControlStateNormal];
        [rightbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [rightbutton addTarget:self action:@selector(addContractAction:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightbutton];
        self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    }
    
}

-(void)addContractAction:(id)sender{
    AddContractViewController *addContractViewController = [[AddContractViewController alloc] init];
    [self.navigationController pushViewController:addContractViewController animated:YES];
}

#pragma mark collectionView代理方法
//返回section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.listArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    MyCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    MenuItem *menuItem = self.listArray[indexPath.row];
    cell.botlabel.text = menuItem.name;
    cell.topImage.image = [UIImage imageNamed:menuItem.url];
    //    FuntionInfo *funtionInfo = self.listArray[indexPath.row];
    //    cell.botlabel.text =funtionInfo.name;
    //    cell.topImage.image = [UIImage imageNamed:[Utils getIconName:funtionInfo.code]];
    //    cell.backgroundColor = [UIColor yellowColor];
    
    return cell;
}

//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH/3, 100);
}

//footer的size
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
//{
//    return CGSizeMake(10, 10);
//}

//header的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(0, 0);
}

//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}


//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}


//通过设置SupplementaryViewOfKind 来设置头部或者底部的view，其中 ReuseIdentifier 的值必须和 注册是填写的一致，本例都为 “reusableView”
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView" forIndexPath:indexPath];
    headerView.backgroundColor =[UIColor grayColor];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:headerView.bounds];
    imageView.image = [UIImage imageNamed:@"mainP"];
    [headerView addSubview:imageView];
    
    
    return headerView;
}

//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    MenuItem *menuItem = self.listArray[indexPath.row];
    if ([menuItem.name isEqualToString:@"广告商合同"]) {
        ShopTypeContractViewController *shopTypeContractViewController = [[ShopTypeContractViewController alloc] initWithType:@"RENT" withName:menuItem.name];
        [self.navigationController pushViewController:shopTypeContractViewController animated:YES];
    }else if ([menuItem.name isEqualToString:@"代理商合同"]){
        ShopTypeContractViewController *shopTypeContractViewController = [[ShopTypeContractViewController alloc] initWithType:@"AGENT" withName:menuItem.name];
        [self.navigationController pushViewController:shopTypeContractViewController animated:YES];
    }else if ([menuItem.name isEqualToString:@"渠道商合同"]){
        ShopTypeContractViewController *shopTypeContractViewController = [[ShopTypeContractViewController alloc] initWithType:@"CHANNEL" withName:menuItem.name];
        [self.navigationController pushViewController:shopTypeContractViewController animated:YES];
    }
    
    else if ([menuItem.name isEqualToString:@"客户合同"]){
        CustomerContractViewController *customerContractViewController = [[CustomerContractViewController alloc] init];
        [self.navigationController pushViewController:customerContractViewController animated:YES];
    }
    //    [self toViewController:funtionInfo.code];
    //    NSString *msg = cell.botlabel.text;
    //    NSLog(@"%@",msg);
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
